package cn.maidouya.user.service.impl;

import cn.maidouya.basic.constants.UserConstants;
import cn.maidouya.basic.exception.BusinessExcepiton;
import cn.maidouya.basic.service.impl.BaseServiceImpl;
import cn.maidouya.basic.util.MD5Utils;
import cn.maidouya.basic.util.StrUtils;
import cn.maidouya.user.domain.Logininfo;
import cn.maidouya.user.domain.User;
import cn.maidouya.user.dto.UserDto;
import cn.maidouya.user.mapper.LogininfoMapper;
import cn.maidouya.user.mapper.UserMapper;
import cn.maidouya.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import sun.security.provider.MD5;

import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    @Transactional
    public void registerPhoneUser(UserDto userDto) {
        // 校验空一、
        if (StringUtils.isEmpty(userDto.getPhone()) || StringUtils.isEmpty(userDto.getVerifyCode()) ||
                StringUtils.isEmpty(userDto.getPassword()) || StringUtils.isEmpty(userDto.getConfirmPwd())){
                throw new BusinessExcepiton("请输入完整注册信息！！");
        }
        // 校验手机号码是否存在、判断手机号码是否存在  手机号码存在判断抛异常UserDto userDto2 =
        User userDto2 = userMapper.loadByPhone(userDto.getPhone());
        if (userDto2 != null){
            throw new BusinessExcepiton("该用户已注册，重新输入手机号码！");
        }
        // 校验密码是否一致、判断输入两次密码是否一致   不一致判断抛异常
        if (!userDto.getPassword().equals(userDto.getConfirmPwd())){
            throw new BusinessExcepiton("输入两次密码不一致！！");
        }
        //  校验验证码A、判断验证码是否一致    不一致判断抛异常
        Object codeObj = redisTemplate.opsForValue().get(UserConstants.USER_REGISTER_CODE_PREFIX + userDto.getPhone());
        if (codeObj == null){
            throw new BusinessExcepiton("验证码不正确，请重新输入!!");
        }
        // Redis验证码结构：user_register_code_prefix:18886530569
        String codeObj1 = ((String) codeObj);
        String code = codeObj1.split(":")[0];
        //  校验验证码B  判断不一致验证码
        if (!code.equalsIgnoreCase(userDto.getVerifyCode())){
            throw new BusinessExcepiton("验证码不正确，请重新输入！！");
        }

        // 注册
        //  UserDto临时对象拷贝到User对象
        User user = userDto2User(userDto);
        //  UserDto临时对象拷贝到Logininfo对象
        Logininfo logininfo = userDto2Logininfo(user);
        // 先保存到关联表
        logininfoMapper.save(logininfo);
        // 获取关联表的id保存到自己的表中     然后执行添加信息
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);
    }
    // 邮箱注册
    @Override
    public void registeremail(UserDto userDto) {
        System.out.println("实现类：" +userDto);
        String email = userDto.getEmail();
        // 第一步 较空
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(userDto.getPassword()) || StringUtils.isEmpty(userDto.getConfirmPwd())){
            throw new BusinessExcepiton("请补充完整信息！");
        }
        // 第二步 校验邮箱账号
        User u = userMapper.loadByEmail(email);
            if (u != null){
                throw new BusinessExcepiton("邮箱存在，直接登录！");
            }
        // 第三步 校验两次密码   equalsIgnoreCase不分区大小写
        if (!userDto.getPassword().equalsIgnoreCase(userDto.getConfirmPwd())){
            throw new BusinessExcepiton("输入两次密码不一致！");
        }
        //  邮箱注册成功
        User user = emailUserDto2User(userDto);
        Logininfo info = emailuserDto2Logininfo(user);
        logininfoMapper.save(info);
         user.setLogininfo_id(info.getId());
         userMapper.save(user);



    }

    private Logininfo emailuserDto2Logininfo(User user) {
        Logininfo info = new Logininfo();
        BeanUtils.copyProperties(user,info);
        info.setType(1);
        return info;
    }

    private User emailUserDto2User(UserDto userDto) {
        User user = new User();
        user.setEmail(userDto.getEmail());
        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String md5 = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(md5);
        return user;
    }


    private Logininfo userDto2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1);
        return logininfo;
    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //  获取随机字符串
        String randomString = StrUtils.getComplexRandomString(32);
        user.setSalt(randomString);

        //  密码加盐值
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + randomString);
       user.setPassword(md5Pwd);
        return user;
    }

}
